package com.ct.config.web.interceptor;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class TestInterceptor implements HandlerInterceptor {
    @Autowired
    private HttpServletRequest request;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Console.log("handler:{}", handler);
        Console.log("handler:{}", handler.getClass());
        Console.log("toStr -> handler:{}", Convert.toStr(handler));

        //获取请求参数
        log.info("getRequestURI:{}", request.getRequestURI());
        //log.info("requestIp:{}", IpUtil.getIpAddr(request));
        log.info("requestPort:{}", request.getServerPort());
        log.info("token:{}", request.getHeader("token"));
        log.info("getMethod:{}", request.getMethod());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
